package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ajk.server.model.Users;
import com.ajk.server.model.query.UsersQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 用户表  <br>
 * t_users <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.UsersMapper
 */
 public class UsersProvider {
 
 	/**
 	* 新增 用户表
 	*/
 	public String insert(Users domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_users(create_time,last_modify_time");
	 	if(domain.getRole()!=null){
			sb.append(",role");
		}
	 	if(domain.getMobile()!=null){
			sb.append(",mobile");
		}
	 	if(domain.getPassword()!=null){
			sb.append(",password");
		}
	 	if(domain.getName()!=null){
			sb.append(",name");
		}
	 	if(domain.getHeight()!=null){
			sb.append(",height");
		}
	 	if(domain.getWeight()!=null){
			sb.append(",weight");
		}
	 	if(domain.getBmi()!=null){
			sb.append(",bmi");
		}
	 	if(domain.getBirthday()!=null){
			sb.append(",birthday");
		}
	 	if(domain.getSex()!=null){
			sb.append(",sex");
		}
	 	if(domain.getDegree()!=null){
			sb.append(",degree");
		}
	 	if(domain.getJob()!=null){
			sb.append(",job");
		}
	 	if(domain.getAvatar()!=null){
			sb.append(",avatar");
		}
	 	if(domain.getRegionId()!=null){
			sb.append(",region_id");
		}
	 	if(domain.getAddress()!=null){
			sb.append(",address");
		}
	 	if(domain.getPastHistory()!=null){
			sb.append(",past_history");
		}
	 	if(domain.getMatrimony()!=null){
			sb.append(",matrimony");
		}
	 	if(domain.getCredits()!=null){
			sb.append(",credits");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
	 	if(domain.getIsBlock()!=null){
			sb.append(",is_block");
		}
	 	if(domain.getHobbys()!=null){
	 		sb.append(",hobbys");
	 	}
	 	if(domain.getVipcodecard()!=null){
	 		sb.append(",vipcodecard");
	 	}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getRole()!=null){
			sb.append(",#{role}");
		}
	 	if(domain.getMobile()!=null){
			sb.append(",#{mobile}");
		}
	 	if(domain.getPassword()!=null){
			sb.append(",#{password}");
		}
	 	if(domain.getName()!=null){
			sb.append(",#{name}");
		}
	 	if(domain.getHeight()!=null){
			sb.append(",#{height}");
		}
	 	if(domain.getWeight()!=null){
			sb.append(",#{weight}");
		}
	 	if(domain.getBmi()!=null){
			sb.append(",#{bmi}");
		}
	 	if(domain.getBirthday()!=null){
			sb.append(",#{birthday}");
		}
	 	if(domain.getSex()!=null){
			sb.append(",#{sex}");
		}
	 	if(domain.getDegree()!=null){
			sb.append(",#{degree}");
		}
	 	if(domain.getJob()!=null){
			sb.append(",#{job}");
		}
	 	if(domain.getAvatar()!=null){
			sb.append(",#{avatar}");
		}
	 	if(domain.getRegionId()!=null){
			sb.append(",#{regionId}");
		}
	 	if(domain.getAddress()!=null){
			sb.append(",#{address}");
		}
	 	if(domain.getPastHistory()!=null){
			sb.append(",#{pastHistory}");
		}
	 	if(domain.getMatrimony()!=null){
			sb.append(",#{matrimony}");
		}
	 	if(domain.getCredits()!=null){
			sb.append(",#{credits}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
	 	if(domain.getIsBlock()!=null){
			sb.append(",#{isBlock}");
		}
	 	if(domain.getHobbys()!=null){
	 		if(StringUtils.isBlank(domain.getHobbys())){
	 			sb.append(",null");
	 		}else{
	 			sb.append(",#{hobbys}");
	 		}
	 	}
	 	if(domain.getVipcodecard()!=null){
	 		sb.append(",#{vipcodecard}");
	 	}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 用户表
 	*/
	public String update(Users domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_users set last_modify_time = now() ");
		
		if(domain.getRole()!=null){
			sb.append(",role=#{role}");
		}
		if(domain.getMobile()!=null){
			sb.append(",mobile=#{mobile}");
		}
		if(domain.getPassword()!=null){
			sb.append(",password=#{password}");
		}
		if(domain.getName()!=null){
			sb.append(",name=#{name}");
		}
		if(domain.getHeight()!=null){
			sb.append(",height=#{height}");
		}
		if(domain.getWeight()!=null){
			sb.append(",weight=#{weight}");
		}
		if(domain.getBmi()!=null){
			sb.append(",bmi=#{bmi}");
		}
		if(domain.getBirthday()!=null){
			sb.append(",birthday=#{birthday}");
		}
		if(domain.getSex()!=null){
			sb.append(",sex=#{sex}");
		}
		if(domain.getDegree()!=null){
			sb.append(",degree=#{degree}");
		}
		if(domain.getJob()!=null){
			sb.append(",job=#{job}");
		}
		if(domain.getAvatar()!=null){
			sb.append(",avatar=#{avatar}");
		}
		if(domain.getRegionId()!=null){
			sb.append(",region_id=#{regionId}");
		}
		if(domain.getAddress()!=null){
			sb.append(",address=#{address}");
		}
		if(domain.getPastHistory()!=null){
			sb.append(",past_history=#{pastHistory}");
		}
		if(domain.getMatrimony()!=null){
			sb.append(",matrimony=#{matrimony}");
		}
		if(domain.getCredits()!=null){
			sb.append(",credits=#{credits}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		if(domain.getIsBlock()!=null){
			sb.append(",is_block=#{isBlock}");
		}
		if(domain.getNursingStationId()!=null){
			sb.append(",nursing_station_id=#{nursingStationId}");
		}
		if(domain.getHobbys()!=null){
	 		if(StringUtils.isBlank(domain.getHobbys())){
	 			sb.append(",is_block=null");
	 		}else{
	 			sb.append(",hobbys=#{hobbys}");
	 		}
	 	}
		if(domain.getVipcodecard()!=null){
	 		sb.append(",vipcodecard=#{vipcodecard}");
	 	}
		if(domain.getQrcode() != null) {
			sb.append(",qrcode=#{qrcode}");
		}
		if(domain.getBloodType() != null) {
			sb.append(",blood_type=#{bloodType}");
		}
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<Users>> map){
		StringBuffer sb = new StringBuffer();
		List<Users> userss = map.get("list");
		sb.append("insert into t_users(create_time,last_modify_time,role,mobile,password,name,height,weight,bmi,birthday,sex,degree,job,avatar,region_id,address,past_history,matrimony,credits,in_used,is_block,vipcodecard) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].role}"
	    		+ ",#'{'list[{0}].mobile}"
	    		+ ",#'{'list[{0}].password}"
	    		+ ",#'{'list[{0}].name}"
	    		+ ",#'{'list[{0}].height}"
	    		+ ",#'{'list[{0}].weight}"
	    		+ ",#'{'list[{0}].bmi}"
	    		+ ",#'{'list[{0}].birthday}"
	    		+ ",#'{'list[{0}].sex}"
	    		+ ",#'{'list[{0}].degree}"
	    		+ ",#'{'list[{0}].job}"
	    		+ ",#'{'list[{0}].avatar}"
	    		+ ",#'{'list[{0}].regionId}"
	    		+ ",#'{'list[{0}].address}"
	    		+ ",#'{'list[{0}].pastHistory}"
	    		+ ",#'{'list[{0}].matrimony}"
	    		+ ",#'{'list[{0}].credits}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+ ",#'{'list[{0}].isBlock}"
	    		+ ",#'{'list[{0}].vipcodecard}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(userss)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  userss.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 用户表
 	*/
	public String findByQuery(UsersQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_users a where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and id=#{userId}");
		}
		if(StringUtils.isNotEmpty(query.getMobile())){
			sb.append(" and mobile=#{mobile}");
		}
		if(StringUtils.isNotEmpty(query.getKeyword())){
			sb.append(" and name like #{keyword}");
		}
		if(query.getRole()!=null){
			sb.append(" and role = #{role}");
		}
		if(IntegerUtil.isPositiveInteger(query.getStartId())){
			sb.append(" and id>#{startId} order by id limit #{limit}");
		}else if(IntegerUtil.isPositiveInteger(query.getEndId())){
			sb.append(" and id<#{endId} order by id desc limit #{limit}");
		}else if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" order by id desc limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(UsersQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_users a where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getUserId())){
			sb.append(" and id=#{userId}");
		}
		if(StringUtils.isNotEmpty(query.getMobile())){
			sb.append(" and mobile=#{mobile}");
		}
		if(StringUtils.isNotEmpty(query.getKeyword())){
			sb.append(" and name like #{keyword}");
		}
		if(query.getRole()!=null){
			sb.append(" and role = #{role}");
		}
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 用户表
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_users where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 用户表
 	*/
 	public String findSimpleByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select avatar,name,id user_id from t_users where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
 }	